part of 'page.dart';

class _AllWordPageView extends StatelessWidget {
  const _AllWordPageView({
    required _AllWordPageWloc wloc,
    super.key,
  }) : _wloc = wloc;

  final _AllWordPageWloc _wloc;

  Widget _buildContent({
    required _AllWordPageWlocState state,
    required BuildContext context,
  }) {
    return Column(
      children: [
        _buildHeader(
          context: context,
        ),
        Expanded(
          child: Stack(
            children: [
              ListView.builder(
                padding: const EdgeInsets.fromLTRB(20, 48, 20, 20),
                physics: const ClampingScrollPhysics(),
                itemCount: state.filteredWords.length,
                itemBuilder: (context, index) {
                  final backgroundColor = index.isEven
                      ? _AllWordPageTheme.of(context).evenTableRowBackgroundColor
                      : _AllWordPageTheme.of(context).oddTableRowBackgroundColor;

                  final word = state.filteredWords[index];

                  return Container(
                    color: backgroundColor,
                    child: _buildTableRow(
                      word: word,
                      index: index,
                      context: context,
                    ),
                  );
                },
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 20,
                  ),
                  child: _buildTableHeader(
                    context: context,
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildTableHeader({
    required BuildContext context,
  }) {
    const kShadowBlurRadius = 3.0;

    return SizedBox(
      height: 48.0,
      child: Container(
        decoration: BoxDecoration(
          color: _AllWordPageTheme.of(context).tableHeaderBackgroundColor,
          boxShadow: [
            BoxShadow(
              color: CoreTheme.of(context).headerDividerColor,
              offset: const Offset(0, kShadowBlurRadius / 2),
              blurRadius: kShadowBlurRadius / 2,
            ),
          ],
        ),
        child: Row(
          children: [
            const Spacer(),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).numberTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).titleTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).partOfSpeechTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).cefrLevelTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).rankTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  _AllWordPageTranslation.of(context).translationTableColumnHeader,
                  style: _AllWordPageTheme.of(context).textTheme.tableHeader,
                  textAlign: TextAlign.start,
                ),
              ),
            ),
            const Spacer(),
          ],
        ),
      ),
    );
  }

  Widget _buildTableRow({
    required Word word,
    required int index,
    required BuildContext context,
  }) {
    return Row(
      children: [
        const Spacer(
          flex: 1,
        ),
        Expanded(
          flex: 15,
          child: MouseRegion(
            cursor: SystemMouseCursors.click,
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: () {
                _wloc.onWordBaseRowPartPressed(
                  wordId: word.id,
                );
              },
              child: Row(
                children: [
                  Expanded(
                    child: _buildNumberCell(
                      index: index,
                      context: context,
                    ),
                  ),
                  Expanded(
                    child: _buildTitleCell(
                      word: word,
                      context: context,
                    ),
                  ),
                  Expanded(
                    child: _buildPartOfSpeechCell(
                      word: word,
                      context: context,
                    ),
                  ),
                  Expanded(
                    child: _buildCefrLevelCell(
                      word: word,
                      context: context,
                    ),
                  ),
                  Expanded(
                    child: _buildRankCell(
                      word: word,
                      context: context,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        Expanded(
          flex: 3,
          child: MouseRegion(
            cursor: SystemMouseCursors.click,
            child: GestureDetector(
              onTap: () {
                _wloc.onWordTranslationRowPartPressed(
                  wordId: word.id,
                );
              },
              child: _buildTranslationCell(
                word: word,
                context: context,
              ),
            ),
          ),
        ),
        const Spacer(
          flex: 1,
        ),
      ],
    );
  }

  Widget _buildNumberCell({
    required int index,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          '${index + 1}',
          style: _AllWordPageTheme.of(context).textTheme.tableCell,
          textAlign: TextAlign.start,
        ),
      ),
    );
  }

  Widget _buildTitleCell({
    required Word word,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          word.title,
          style: _AllWordPageTheme.of(context).textTheme.tableCell,
          textAlign: TextAlign.start,
        ),
      ),
    );
  }

  Widget _buildPartOfSpeechCell({
    required Word word,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          _wloc.getWordPartOfSpeechLabel(
            word: word,
          ),
          style: _AllWordPageTheme.of(context).textTheme.tableCell,
          textAlign: TextAlign.start,
        ),
      ),
    );
  }

  Widget _buildCefrLevelCell({
    required Word word,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          _wloc.getWordCefrLevelLabel(
            word: word,
          ),
          style: _AllWordPageTheme.of(context).textTheme.tableCell,
          textAlign: TextAlign.start,
        ),
      ),
    );
  }

  Widget _buildRankCell({
    required Word word,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Text(
          '${word.rank}',
          style: _AllWordPageTheme.of(context).textTheme.tableCell,
          textAlign: TextAlign.start,
        ),
      ),
    );
  }

  Widget _buildTranslationCell({
    required Word word,
    required BuildContext context,
  }) {
    return _wrapCell(
      context: context,
      child: Align(
        alignment: Alignment.centerLeft,
        child: Padding(
          padding: const EdgeInsets.symmetric(
            vertical: 8,
          ),
          child: Text(
            word.translations[LearningLocale.ru]!,
            style: _AllWordPageTheme.of(context).textTheme.tableCell,
            textAlign: TextAlign.start,
          ),
        ),
      ),
    );
  }

  Widget _wrapCell({
    required Widget child,
    required BuildContext context,
  }) {
    return ConstrainedBox(
      constraints: const BoxConstraints(
        minHeight: 48,
      ),
      child: child,
    );
  }

  Widget _buildHeader({
    required BuildContext context,
  }) {
    return Column(
      children: [
        Container(
          height: 60,
          width: double.infinity,
          color: CoreTheme.of(context).headerBackgroundColor,
        ),
        Divider(
          color: CoreTheme.of(context).headerDividerColor,
          height: 0,
          thickness: 1.0,
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: double.infinity,
      width: double.infinity,
      color: CoreTheme.of(context).backgroundColor,
      child: WlocBuilder<_AllWordPageWloc, _AllWordPageWlocState>(
        wloc: _wloc,
        builder: (context, state) {
          return _buildContent(
            state: state,
            context: context,
          );
        },
      ),
    );
  }
}
